package edu.tyut.game.controller.email;

import edu.tyut.game.model.User;
import com.alibaba.fastjson2.JSON;
import edu.tyut.game.model.Result;
import edu.tyut.game.service.EmailService;
import edu.tyut.game.utils.VerificationCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/email")
public class EmailController {

    private EmailService emailService;
    @Autowired
    public void setEmailService(EmailService emailService) {
        this.emailService = emailService;
    }

    @RequestMapping("/sendEmail")
    public Result<String> sendMail(@RequestBody String userJson){
        try {
            User user = JSON.parseObject(userJson, User.class);
            String subject = "注册邮箱验证码";
            String content = "欢迎注册飞花令APP账号!";
            String template = "/mail/emailVerify";
            String verificationCode = VerificationCode.getVerificationCode();
            emailService.sendEmailVerifyMessage(subject, content, template, verificationCode, user);
            log.debug("发送邮箱成功, 验证码: " + verificationCode);
            return Result.success("发送验证码成功", verificationCode);
        }catch (Exception  e){
            log.error("发送邮箱失败：" + e.getMessage());
            return Result.error("发送验证码失败", null);
        }
    }

    @RequestMapping("/sendRetrievePasswordEmail")
    public Result<String> sendRetrievePasswordEmail(@RequestBody User user){
        try {
            String subject = "找回密码邮箱验证码";
            String content = "欢迎使用飞花令APP账号密码找回功能!";
            String template = "/mail/emailRetrievePassword";
            String verificationCode = VerificationCode.getVerificationCode();
            emailService.sendEmailVerifyMessage(subject, content, template, verificationCode, user);
            log.debug("发送邮箱成功, 验证码: " + verificationCode);
            return Result.success("发送验证码成功", verificationCode);
        }catch (Exception  e){
            log.error("发送邮箱失败：" + e.getMessage());
            return Result.error("发送验证码失败", null);
        }
    }
}
